/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import entities.Room;
import hotel.jpa.Database;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import model.AddBackingBean;


/**
 * A class to controll the AddRoom
 * @author Anton Karlsson
 */
@ManagedBean(name="addRoomControllerBean")
public class AddControllBean {
    
    public void addRoom(){
        
        //Using other bean (addRoomBackingBean)
        FacesContext ctx = FacesContext.getCurrentInstance();
        Application app = ctx.getApplication();
        ELContext ectx = ctx.getELContext();
        ExpressionFactory ef = app.getExpressionFactory();
        ValueExpression ve = ef.createValueExpression(ectx, "#{addRoomBackingBean}", AddBackingBean.class);
        AddBackingBean db = (AddBackingBean) ve.getValue(ectx);
        
        //Creates the new room
        Room r = new Room(db.getRoomNb(), db.getBathRoom(), db.getKitchen(), db.getPrice(), db.getRoomType());
        
        //Persist it to database
        Database.getRoomController().create(r);
    }
  
}
